package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult<User> list(UserDto userDto) {
        PageHelper.startPage(userDto.getPage(),userDto.getPageSize());
        List<User> list = userMapper.selectList(userDto);
        Page<User> page = (Page<User>) list;
        return new PageResult<>(page.getTotal(),page.getResult());
    }

    @Override
    public void addUser(User user) {
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());

        String password = user.getPassword()+"123";
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        userMapper.addUser(user);
    }

    @Override
    public void deleteById(List<String> ids) {
        userMapper.DeleteById(ids);
    }
}
